using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TeamTime.Classes;
using StillVoiceCommon;
namespace TeamTime
{
    public partial class TasksForm : System.Web.UI.Page //TeamTime.Classes.CompressedPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //Companies companies = new Companies();
                
                // this.txtFromDate.Text = DateTime.Now.AddDays(-7).ToString("yyyy/MM/dd"); //default to 1 weeks worth of Batches
                GridView1.Attributes.Add("SortExpression", "ContactDate desc");
                GridView1.Attributes.Add("SortExpression", "ID");
               
                //if (Functions.IsNumeric(Request.QueryString["ID"]))
                //{
                //    this.ContactRecordFormControl1.ContactRecordID = Convert.ToInt32( Request.QueryString["ID"]);
                //    //LoadDetailsForm(this.txtID.Text);
                //    this.txtSearchFor.Text = this.ContactRecordFormControl1.CompContactRecordSurname;
                //    popUp1.Show();
                //}
                //else if (Request.QueryString["CompanyID"] != null)
                //{
                //    this.ContactRecordFormControl1.
                //}
                //DateTime dt = DateTime.Today.AddDays(-1 * (DateTime.Today.Day - 1));
                //this.txtFromDate.Text = dt.AddMonths(0).ToString("yyyy/MM/dd"); // First day of last month
                //DateTime month = dt.AddMonths(1);
                //this.txtToDate.Text = month.AddDays(-1).ToString("yyyy/MM/dd"); // Last day of last month
                this.ddlStatus.SelectedValue = "Not completed";
                if (Functions.IsNumeric(Request.QueryString["CompanyID"]))
                {
                    Companies companies = new Companies();
                    this.txtCompanyNameSearch.Text = companies.getCompanyNameByID(Convert.ToInt32(Request.QueryString["CompanyID"]));
                }
                if (Functions.IsNumeric(Request.QueryString["ProjectName"]))
                {
                    TN5YProjects tn5yProjects = new TN5YProjects();
                    this.txtProjectSearch.Text = tn5yProjects.getProjectNameByID(Convert.ToInt32(Request.QueryString["ProjectName"]));
                }
                
                EnableControls();
                
                StaffMembers staffMembers = new StaffMembers();

                Functions.dropDownListPopulate(this.ddlStaffMemberIDAllocatedTo, staffMembers.getStaffMembers(true), "ID", "FullName");
                Users user = new Users();
                Int32 PersonID = user.getPersonIDFromUser(Session["UserID"].ToString());
                ddlStaffMemberIDAllocatedTo.SelectedValue = Convert.ToString(user.getIDFromViewStaffMembers(PersonID)); 
                btnSearch_Click(sender, e);
                this.Form.DefaultFocus = this.txtSearchFor.ClientID;
                this.Form.DefaultButton = this.btnSearch.UniqueID;
                if (Request.QueryString["mode"] == "add")
                    btnAddNew_Click(sender, e);
            }

        }

        private void EnableControls()
        {

            Functions.enableChildControls(this.Form);
            
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        public DateTime fromdate;
        public DateTime todate;        
        private void BindGrid()
        {
            int ownerCompanyID = 0;
            Owners owners = new Owners();
            if (txtCompanyNameSearch.Text != "")
            {

                 ownerCompanyID = owners.getCompanyIDByName(this.txtCompanyNameSearch.Text);
                if (ownerCompanyID == 0)
                {
                    lblWrongCompany.ForeColor = System.Drawing.Color.Red;
                    lblWrongCompany.Text = "Please select a valid Company";
                    return;
                }
            }

            int ProjectID = 0;
            TN5YProjects project = new TN5YProjects();
            if (txtProjectSearch.Text != "")
            {

                ProjectID = project.getProjectIDByName(this.txtProjectSearch.Text);
                if (ProjectID == 0)
                {
                    lblWrongProject.ForeColor = System.Drawing.Color.Red;
                    lblWrongProject.Text = "Please select a valid Project";
                    return;
                }
            }

            

                        
            Tasks tasks = new Tasks();

            if (txtFromDate.Text == string.Empty)
            {
                fromdate = Convert.ToDateTime("1999-09-09");
            }
            else { fromdate = Convert.ToDateTime(txtFromDate.Text); }
            if (txtToDate.Text == string.Empty)
            {
                todate = Convert.ToDateTime("3000-03-03");
            }
            else { todate = Convert.ToDateTime(txtToDate.Text); }

            this.GridView1.DataSource = tasks.search(this.txtSearchFor.Text, fromdate, todate, this.ddlStatus.SelectedValue, Convert.ToInt32(this.ddlStaffMemberIDAllocatedTo.SelectedValue),ownerCompanyID, ProjectID);
            
            this.GridView1.DataBind();
           
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            BindGrid();
            TaskFormControl1.setForAdding();
           
            //if (this.txtCompanyNameSearch.Text != "")
            //    ((TextBox)this.TaskFormControl1.FindControl("pnlDetails").FindControl("txtCompanyName")).Text = this.txtCompanyNameSearch.Text;
            popUp1.Show();
           
        }

        protected void ContactRecordFormControl1_SaveClicked(object sender, EventArgs e)
        {
            popUp1.Show();
            BindGrid();
        }
        protected void ContactRecordFormControl1_LoadDupClicked(object sender, EventArgs e)
        {
            popUp1.Show();
        }

        //Grid Methods
        protected void GridView_Sort(object source, DataGridSortCommandEventArgs e)
        {

            GridView1.Attributes.Add("SortExpression", e.SortExpression);
            BindGrid();
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string id = e.CommandArgument.ToString();
                TaskFormControl1.TaskID = Convert.ToInt32(id);
                popUp1.Show();
            }
            if (e.CommandName == "Add")
            {
              
                Response.Redirect("TimeEntriesForm.aspx?TaskID="+e.CommandArgument.ToString());
            }
            if (e.CommandName == "Description")
            {
                string id = e.CommandArgument.ToString();
                Tasks tasks = new Tasks();
                DescriptionPopupControl1.Description = tasks.getTaskDescription( Convert.ToInt32(id));
                popUp2.Show();
            }
          
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGrid = sender as GridView;  // refer to the GridView
            int newPageIndex = 0;

            if (-2 == e.NewPageIndex)
            { // when click the "GO" Button
                TextBox txtNewPageIndex = null;
                //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                GridViewRow pagerRow = theGrid.BottomPagerRow; //


                if (null != pagerRow)
                {
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                }

                if (null != txtNewPageIndex)
                {
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                }
            }
            else
            {  // when click the first, last, previous and next Button
                newPageIndex = e.NewPageIndex;
            }

            // check to prevent form the NewPageIndex out of the range
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            // specify the NewPageIndex
            theGrid.PageIndex = newPageIndex;

            // display a new page of data
            // GridView1.EditIndex = -1;
            //GridView1.PageIndex = e.NewPageIndex;
            BindGrid();
            // rebind the control
            // in this case of retrieving the data using the xxxDataSoucr control,
            // just do nothing, because the asp.net engine binds the data automatically
        }


        protected void TaskFormControl1_SaveClicked(object sender, EventArgs e)
        {
            //popUp1.Show();
            BindGrid();
        }
        protected void TaskFormControl1_LoadDupClicked(object sender, EventArgs e)
        {
            popUp1.Show();
            
        }
       

    }
   

}



